package unitTests;

import static org.junit.Assert.*;

import java.util.TreeMap;
import java.util.Vector;

import org.junit.Before;
import org.junit.Test;

import forumSystem.Forum;
import forumSystem.ForumSystemImpl;

public class TestSystemImpl {
	
	private TreeMap<String, String> session;
	String adminUser;
	String adminPass;
	/**
	 * Before every test
	 * we initialize the forum with
	 * admin user: admin
	 * admin pass: pass
	 * @throws Exception
	 */
	@Before
	public void setUp() throws Exception {
		adminUser="admin";
		adminPass="pass";
		ForumSystemImpl.initForum(adminUser	,adminPass);
		this.session=new TreeMap<String, String>();
	}
	
	
	@Test
	public void testLogOut() {
		int ans;
		// guest
		ans=ForumSystemImpl.logOut(session);
		assertEquals(ans, -1);
		ForumSystemImpl.adminLogin(adminUser, adminPass	, session);
		int policyID = ForumSystemImpl.createPolicy(1, 1, 1, 1, 1, 1, 1, 1, session);
		int forumID = ForumSystemImpl.createForum("Animals", ans, policyID, session);
		// admin
		ans=ForumSystemImpl.logOut(session);
		assertFalse(ans==-1);
		int userid=ForumSystemImpl.register("or", "hemi", "orhemi@walla.com", forumID, session);
		ForumSystemImpl.logIn("or", "hemi", forumID, session);
		//user
		ans=ForumSystemImpl.logOut(session);
		assertFalse(ans==-1);
	}

	@Test
	public void testLogIn() {
		int[] ans;
		ForumSystemImpl.adminLogin(adminUser, adminPass	, session);
		int policyID = ForumSystemImpl.createPolicy(1, 1, 1, 1, 1, 1, 1, 1, session);
		int forumID = ForumSystemImpl.createForum("Animals", 1, policyID, session);
		ForumSystemImpl.logOut(session);
		
		// user does not exist
		ans=ForumSystemImpl.logIn("or", "hemi", forumID, session);
		assertEquals(ans, null);
		
		int userid=ForumSystemImpl.register("or", "hemi", "orhemi@walla.com", forumID, session);
		// wrong username
		ans = ForumSystemImpl.logIn("or222", "hemi", forumID, session);
		assertEquals(ans, null);
		
		// wrong pass
		ans = ForumSystemImpl.logIn("or", "hemi222", forumID, session);
		assertEquals(ans, null);
		
		// wrong forumid
		ans = ForumSystemImpl.logIn("or", "hemi", 50, session);
		assertEquals(ans, null);
		
		// correct login
		ans = ForumSystemImpl.logIn("or", "hemi", forumID, session);
		assertFalse(ans == null);
		
	}

	@Test
	public void testRegister() {
		int ans;
		ForumSystemImpl.adminLogin(adminUser, adminPass	, session);
		int policyID = ForumSystemImpl.createPolicy(1, 1, 1, 1, 1, 1, 1, 1, session);
		int forumID = ForumSystemImpl.createForum("Animals", 1, policyID, session);
		ForumSystemImpl.logOut(session);
		
		// email not valid
		ans=ForumSystemImpl.register("or", "hemi", "orhemi", forumID, session);
		assertEquals(ans, -1);
		
		// wrong forumid
		ans=ForumSystemImpl.register("or", "hemi", "orhemi@walla.com", 50, session);
		assertEquals(ans, -1);
		
		// correct register
		ans=ForumSystemImpl.register("or", "hemi", "orhemi@walla.com", forumID, session);
		assertFalse(ans == -1);
		
		// while logged in
		ForumSystemImpl.logIn("or", "hemi", forumID, session);
		ans=ForumSystemImpl.register("or1", "hemi", "orhemi@walla.com", forumID, session);
		assertEquals(ans, -1);
		
	}

	@Test
	public void testGetAllSubForums() {
		Vector<String> ans;
		ForumSystemImpl.adminLogin(adminUser, adminPass	, session);
		int policyID = ForumSystemImpl.createPolicy(1, 1, 1, 1, 1, 1, 1, 1, session);
		int forumID = ForumSystemImpl.createForum("Animals", 1, policyID, session);
		
		// wrong forum id
		ForumSystemImpl.createSubForum("Blowfish", 50, 1, session);
		ans =ForumSystemImpl.getAllSubForums(forumID);
		assertEquals(ans.size(), 0);
		
		// no subforums
		ans =ForumSystemImpl.getAllSubForums(forumID);
		assertEquals(ans.size(), 0);
		
		// 1 subforum
		ForumSystemImpl.createSubForum("Blowfish", forumID, 1, session);
		ans =ForumSystemImpl.getAllSubForums(forumID);
		assertEquals(ans.size(), 2);
		assertEquals(ans.get(0), "Blowfish");
		

	}

	@Test
	public void testGetForumUsers() {
		Vector<Integer> ans;
		ForumSystemImpl.adminLogin(adminUser, adminPass	, session);
		int policyID = ForumSystemImpl.createPolicy(1, 1, 1, 1, 1, 1, 1, 1, session);
		int forumID = ForumSystemImpl.createForum("Animals", 1, policyID, session);
		
		// empty forum
		ans = ForumSystemImpl.getForumUsers(forumID);
		assertEquals(ans.size(), 0);
		
		ForumSystemImpl.logOut(session);
		int userid = ForumSystemImpl.register("or", "hemi", "orhemi@walla.com", forumID, session);
		
		// wrong forum id
		ans = ForumSystemImpl.getForumUsers(50);
		assertEquals(ans.size(), 0);
		
		// 1 user
		ans = ForumSystemImpl.getForumUsers(forumID);
		assertEquals(ans.size(), 1);
		assertTrue(ans.get(0)==userid);

	}

	@Test
	public void testIsMailInUse() {
		boolean ans;
		ForumSystemImpl.adminLogin(adminUser, adminPass	, session);
		int policyID = ForumSystemImpl.createPolicy(1, 1, 1, 1, 1, 1, 1, 1, session);
		int forumID = ForumSystemImpl.createForum("Animals", 1, policyID, session);
		
		// not in use
		ans = ForumSystemImpl.isMailInUse("orhemi@walla.com", forumID);
		assertFalse(ans);
		
		// in used
		ForumSystemImpl.logOut(session);
		ForumSystemImpl.register("or", "hemi", "orhemi@walla.com", forumID, session);
		ans = ForumSystemImpl.isMailInUse("orhemi@walla.com", forumID);
		assertTrue(ans);
	}
	
	@Test
	public void testAddAdmin(){
		int ans;
		ForumSystemImpl.adminLogin(adminUser, adminPass	, session);
		int policyID = ForumSystemImpl.createPolicy(1, 1, 1, 1, 1, 1, 1, 1, session);
		int forumID = ForumSystemImpl.createForum("Animals", 1, policyID, session);
		
		// wrong admin id
		ans = ForumSystemImpl.addAdmin(50, forumID, session);
		assertEquals(ans, -1);
		
		ForumSystemImpl.logOut(session);
		int userId = ForumSystemImpl.register("or", "hemi", "orhemi@walla.com", forumID, session);
		ForumSystemImpl.adminLogin(adminUser, adminPass	, session);
		
		// wrong forum id
		ans = ForumSystemImpl.addAdmin(userId, 50, session);
		System.out.println(ans);
		assertEquals(ans, -1);
		
		//correct
		ans = ForumSystemImpl.addAdmin(userId, forumID, session);
		assertEquals(ans, 1);
	}
	
	@Test
	public void testGetForumByID(){
		ForumSystemImpl.adminLogin(adminUser, adminPass	, session);
		int policyID = ForumSystemImpl.createPolicy(1, 1, 1, 1, 1, 1, 1, 1, session);
		int forumID = ForumSystemImpl.createForum("Animals", 1, policyID, session);
		
		Forum f=ForumSystemImpl.getForumById(forumID);
		assertEquals(f.getName(), "Animals");
		assertEquals(f.getAdminID(), 1);
	}
	

}
